package com.autoMobile.dao;

import com.autoMobile.bean.Vehicle;
import com.autoMobile.bean.auto_management;
import com.autoMobile.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class auto_managementDao {

    // 插入新车辆信息
    public static int addVehicle(Vehicle vehicle) {
        Connection conn = null;
        PreparedStatement psts = null;
        int affectedRows = 0;
        String sql = "INSERT INTO Vehicles (licensePlate, model, status, currentDriver, purchaseDate, totalMileage) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            System.out.println(vehicle.toString());
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            psts.setString(1, vehicle.getLicensePlate());
            psts.setString(2, vehicle.getModel());
            psts.setString(3, vehicle.getStatus());
            psts.setString(4, vehicle.getCurrentDriver());
            psts.setDate(5, (Date) vehicle.getPurchaseDate());
            psts.setInt(6, vehicle.getTotalMileage());
            affectedRows = psts.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, null);
        }
        return affectedRows; // 通常返回1表示插入成功，但这里保留为int类型以处理可能的数据库特性
    }

    // 更新车辆信息
    public static int updateVehicle(Vehicle vehicle) {
        Connection conn = null;
        PreparedStatement psts = null;
        int affectedRows = 0;
        String sql = "UPDATE Vehicles SET model=?, status=?, currentDriver=?, purchaseDate=?, totalMileage=? WHERE licensePlate=?";
        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            psts.setString(1, vehicle.getModel());
            psts.setString(2, vehicle.getStatus());
            psts.setString(3, vehicle.getCurrentDriver());
            psts.setDate(4, (Date) vehicle.getPurchaseDate());
            psts.setInt(5, vehicle.getTotalMileage());
            psts.setString(6, vehicle.getLicensePlate());
            affectedRows = psts.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, null);
        }
        return affectedRows;
    }

    // 删除车辆信息
    public static int deleteVehicle(int id) {
        Connection conn = null;
        PreparedStatement psts = null;
        int affectedRows = 0;
        String sql = "DELETE FROM Vehicles WHERE id=?";
        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            psts.setInt(1, id);
            affectedRows = psts.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, null);
        }
        return affectedRows;
    }

    // 查询所有车辆信息
    public static List<auto_management> getAutoManagement() {
        Connection conn = null;
        PreparedStatement psts = null;
        ResultSet rs = null;
        List<auto_management> auto_managements = new ArrayList<auto_management>();
        String sql = "SELECT * FROM auto_management";
        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            rs = psts.executeQuery();
            while (rs.next()) {
                if (rs.getString(14).equals("申请")  || rs.getString(14).equals("还车登记")){
                    auto_management auto_management = new auto_management(rs.getInt(1),rs.getString(2), rs.getString(3),
                            rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8),
                            rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13),
                            rs.getString(14));
                    auto_managements.add(auto_management);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, rs);
        }
        return auto_managements;
    }

    // 根据车牌号查询车辆信息
    public static Vehicle getVehicleByLicensePlate(String licensePlate) {
        Connection conn = null;
        PreparedStatement psts = null;
        ResultSet rs = null;
        Vehicle vehicle = null;
        String sql = "SELECT * FROM Vehicles WHERE licensePlate=?";
        try {
            conn = DBUtil.getConnection();
            psts = conn.prepareStatement(sql);
            psts.setString(1, licensePlate);
            rs = psts.executeQuery();
            if (rs.next()) {
                vehicle = new Vehicle();
                vehicle.setLicensePlate(rs.getString(1));
                vehicle.setModel(rs.getString(2));
                vehicle.setStatus(rs.getString(3));
                vehicle.setCurrentDriver(rs.getString(4));
                vehicle.setPurchaseDate(rs.getDate(5));
                vehicle.setTotalMileage(rs.getInt(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, psts, rs);
        }
        return vehicle;
    }
}